<?php
namespace app\api\controller;

use think\Request;
use app\common\model\User as UserModel;
use app\exception\BusinessException;
use app\common\service\JwtService;
class User
{
    // 注册接口（已移除验证逻辑）
    public function register(Request $request)
    {   
        $data = array_merge($request->param(),['create_ip'=>$request->ip()]);
        $user = UserModel::create($data);
        UserModel::destroy(80);
        return json(['code' => 200, 'data' => $user->visible(['user_id', 'phone'])]);
    }

    // 登录接口（已移除验证逻辑）
    public function login()
    {
        $credentials = request()->param();
        
        // 验证用户凭证
        $user = UserModel::where('phone', $credentials['phone'])
            ->findOrFail();
        
        if (!password_verify($credentials['password'], $user->password)) {
            throw new BusinessException('密码错误', 401);
        }

        // 生成令牌
        $token = app(JwtService::class)->generateToken($user->user_id, [
            'phone' => $user->phone
        ]);

        return json([
            'access_token' => $token,
            'expires_in'   => config('jwt.ttl')
        ]);
    }
        public function refresh()
    {
        $token = app(JwtService::class)
            ->refreshToken(request()->header('Authorization'));
        
        return json([
            'access_token' => $token,
            'expires_in'   => config('jwt.ttl')
        ]);
    }

}
